package com.usian.article.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.mapper.ArticleConfigMapper;
import com.usian.common.constants.NewsConstant;
import com.usian.model.article.ApArticleConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

//@Component
public class ArticleIsDownListener {

    @Autowired
    private ArticleConfigMapper configMapper;
    @KafkaListener(topics = NewsConstant.WM_NEWS_UP_OR_DOWN)
    public void getMessage(ConsumerRecord record){
        if (record != null && record.value() != null){
            String json = (String) record.value();
            JSONObject object = JSON.parseObject(json);
            long articleId = object.getLongValue("articleId");
            short enable = object.getShortValue("enable");
            configMapper.update(null, Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId,articleId).set(ApArticleConfig::getIsDown,enable));
        }
    }
}
